package com.atguigu.mySmartCampus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConf {

    @Bean
    public Docket WebApiConfig(){
        List<Parameter> paramList = new ArrayList();
        ParameterBuilder tokenParameterBuilder = new ParameterBuilder();
        ParameterBuilder name = tokenParameterBuilder.name("userId");
        ParameterBuilder description = name.description("用户ID");
        ParameterBuilder parameterBuilder = description.defaultValue("1");
        ParameterBuilder parameterBuilder1 = parameterBuilder.modelRef(new ModelRef("string"));
        ParameterBuilder parameterBuilder2 = parameterBuilder1.parameterAccess("header");
        ParameterBuilder required = parameterBuilder2.required(false);
        Parameter build = required.build();
        paramList.add(build);

        ParameterBuilder tempTokenParameterBuilder = new ParameterBuilder().name("userTempId")
        .description("用户临时ID")
        .defaultValue("1")
        .modelRef(new ModelRef("string"))
        .parameterAccess("header")
        .required(false);

        Parameter build2 = tempTokenParameterBuilder.build();
        paramList.add(build2);

        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        Docket webApi = docket.groupName("webApi");
        Docket docket1 = webApi.apiInfo(webApiInfo());
        ApiSelectorBuilder select = docket1.select();
        ApiSelectorBuilder apis = select.apis(RequestHandlerSelectors.basePackage("com.atguigu.mySmartCampus"));
        Docket build1 = apis.build();
        Docket docket2 = build1.globalOperationParameters(paramList);
        return docket2;
    }

    private ApiInfo webApiInfo(){
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        ApiInfoBuilder apiInfoBuilder2 = apiInfoBuilder.title("网站api 技术文档");
        ApiInfoBuilder apiInfoBuilder3 = apiInfoBuilder2.description("描述微服务接口定义");
        ApiInfoBuilder version = apiInfoBuilder3.version("1.0");
        ApiInfoBuilder atguigu = version.contact(new Contact("atguigu", "http://atguigu.com", "850490791@qq.com"));
        ApiInfo build = atguigu.build();
        return build;
    }

    private ApiInfo adminApiInfo(){
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        ApiInfoBuilder apiInfoBuilder2 = apiInfoBuilder.title("后台管理系统api 技术文档");
        ApiInfoBuilder apiInfoBuilder3 = apiInfoBuilder2.description("描述后台管理系统接口定义");
        ApiInfoBuilder version = apiInfoBuilder3.version("1.0");
        ApiInfoBuilder atguigu = version.contact(new Contact("atguigu", "http://atguigu.com", "850490791@qq.com"));
        ApiInfo build = atguigu.build();
        return build;
    }
}
